home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / macros / latex209 / contrib / psfrag / ps2psfrag / ps2psfrag.ps < prev    next >
Text File  |  1992-09-19  |  5KB  |  165 lines

  1. %! PS-Adobe-2.0
  2. %
  3. % PostScript program for reading a .ps file and writing out a LaTeX
  4. % file that contains information about each piece of text shown by the
  5. % ps file.
  6. % GhostScript Usage:
  7. %
  8. %    dx dy (fileName) FragConvert
  9. %
  10. %    Reads fileName.ps and writes latex information to fileName.psfrag
  11. %    The show text coordinates are offset by dx, dy.
  12. %
  13. % Revision History:
  14. %   20-Sep-92   Craig Barratt    Released version 1.1.
  15. %   13-Sep-92   Craig Barratt    Added support for ashow, widthshow, awidthshow,
  16. %                and kshow.
  17. %   21-Jul-92   Craig Barratt    Correctly handle empty show strings.  Added
  18. %    9-Jun-92   Craig Barratt    Changed egrep to grep for system V users.
  19. %    1-Jun-92   Craig Barratt    Released version 1.0.
  20. %   23-Feb-92   Craig Barratt    Initial version.
  21. %
  22. % Send comments and bugs to Craig Barratt (craig@isl.stanford.edu)
  23. %
  24.  
  25. /fragdict 30 dict def
  26.  
  27. fragdict begin
  28.  
  29. % write a string to the output file
  30. /w { FragOutFile exch writestring } bind def
  31. % write an argument to the output file
  32. /warg { ({) w w (}) w } bind def
  33. /p { 16 string cvs print flush } def
  34. /sp { ( ) p } def
  35.  
  36. %
  37. % writes out latex information into the psfrag file for
  38. % each show operator
  39. %
  40. /FragProcessShow {
  41.     FragPass 1 eq
  42.     {
  43.     % pass 1: write out just the show string
  44.     /s exch def
  45.     % we only care about non-empty show strings
  46.     s () eq not
  47.     {
  48.         s (\\tex) anchorsearch
  49.         {
  50.         pop pop
  51.         }
  52.         {
  53.         pop
  54.         (    \\PsFragShowString) w s warg (\n) w
  55.         } ifelse
  56.     } if
  57.     }
  58.     {
  59.     % pass 2: write out psfrag info
  60.     /s exch def
  61.     % we only care about non-empty show strings
  62.     s () eq not
  63.     {
  64.         gsave
  65.         matrix currentmatrix /origMatrix exch def
  66.         currentpoint /origy exch def /origx exch def
  67.         % write out the string as the first arg
  68.         s (\\tex) anchorsearch {
  69.         % define this fragment using an empty label
  70.         pop (    \\psfrag{}) w w (\n) w
  71.         % now immediately refer to the empty label
  72.         (    \\PsFragInfo{}) w
  73.         }
  74.         {
  75.         pop (    \\PsFragInfo) w s warg
  76.         } ifelse
  77.         % write out the current point in abs coords as the next two args
  78.         initmatrix currentpoint /abslly exch def /absllx exch def
  79.         absllx FragDx add 30 string cvs warg
  80.         abslly FragDy add 30 string cvs warg
  81.         % get width, height and orientation of the string
  82.         origMatrix setmatrix
  83.         newpath 0 0 moveto s true charpath pathbbox
  84.         /ury exch def /urx exch def pop pop
  85.         origx origy moveto urx 0 rmoveto
  86.         initmatrix currentpoint /abslry exch def /abslrx exch def
  87.         origMatrix setmatrix
  88.         origx origy moveto 0 ury rmoveto
  89.         initmatrix currentpoint /absuly exch def /absulx exch def
  90.  
  91.         % write out dx and dy for the lower left to lower right vector
  92.         abslrx absllx sub 30 string cvs warg
  93.         abslry abslly sub 30 string cvs warg
  94.  
  95.         % write out dx and dy for the lower left to upper left vector
  96.         absulx absllx sub 30 string cvs warg
  97.         absuly abslly sub 30 string cvs warg
  98.  
  99.         % write out the text angle (keep it between -180 and 180,
  100.         % and round to the nearest 0.01)
  101.         abslry abslly sub abslrx absllx sub
  102.         % avoid atan(0,0): check if both numbers are zero
  103.         % (bug reported by Noel Doughty nad@phys.canterbury.ac.nz)
  104.         2 copy abs exch abs add
  105.         0 eq { pop pop 0 } { atan } ifelse
  106.         dup 360 div round 360 mul sub
  107.         100 mul round 100 div 30 string cvs warg
  108.  
  109.         % finish up
  110.         (\n) w
  111.         origMatrix setmatrix
  112.         origx origy moveto
  113.         grestore
  114.     } if
  115.     } ifelse
  116. } bind def
  117.  
  118. end
  119.  
  120. userdict begin
  121.  
  122. %
  123. % redefine all the show operators to call FragProcessShow,
  124. % and then do the normal show
  125. %
  126. /widthshow  { dup fragdict begin FragProcessShow end widthshow  } bind def
  127. /awidthshow { dup fragdict begin FragProcessShow end awidthshow } bind def
  128. /ashow      { dup fragdict begin FragProcessShow end ashow      } bind def
  129. /kshow      { dup fragdict begin FragProcessShow end kshow      } bind def
  130. /show       { dup fragdict begin FragProcessShow end show       } bind def
  131.  
  132. /FragConvert {
  133.     fragdict begin
  134.     /FragInFileName exch def
  135.     FragInFileName (frag) concatstrings (w) file /FragOutFile exch def
  136.     /FragDy exch def
  137.     /FragDx exch def
  138.     (\%\n\% File automatically generated by ps2psfrag and GhostScript.\n) w
  139.     (\% It is included by \\epsfbox or \\psfragspecial.\n) w
  140.     (\% Do not \\input directly, or edit!\n\%\n) w
  141.     (\\begin{PSFragmentLocs}\n) w
  142.     %
  143.     % I'm really lazy, so we run through the ps file twice.
  144.     % The first pass generates a list of \PsFragShowString{} calls.
  145.     % These are used by latex to check if the strings is being replaced.
  146.     % The second pass generates a list of \PsFragInfo{} calls, which
  147.     % contains complete information about each ps fragment.
  148.     %
  149.     /FragPass 1 def
  150.     gsave /FragVM save def
  151.         FragInFileName userdict begin run end
  152.     FragVM restore grestore
  153.     (    \\put(0,0){\\special{\\PsFragSpecialArgs}}\n) w
  154.     /FragPass 2 def
  155.     gsave /FragVM save def
  156.         FragInFileName userdict begin run end
  157.     FragVM restore grestore
  158.     (\\end{PSFragmentLocs}\n) w
  159.     FragOutFile closefile
  160.     end
  161. } bind def
  162.  
  163. end
  164.